//
//  MYGDoctorSelectViewController.swift
//  MYDoctor
//
//  Created by 王振明 on 2024/4/26.
//

import UIKit
import JXSegmentedView

class MYGDoctorSelectViewController: MYGBaseController,UITextFieldDelegate {
    var caseModel:MYGCaseClasmListModel?
    var leftVc:MYGDoctorSelectDetailListViewController?
    var rightVc:MYGSelectAllDorctorsViewController?
    var currrentIndex:Int = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarShow()
        self.title = "选择医生"
        initUI()
    }
    @objc private func sureAction(){
        var doctorId:Int = 0
        if currrentIndex == 0{
            doctorId = leftVc?.selectDoc.from_doctor.id ?? 0
        }else{
            doctorId = rightVc?.selectDoc.id ?? 0
        }
        MYGLoading.show()
        MYGNetwork<MYGMeAPI,MYGEmptyModel>.request(target: .sharePatient(param: [
            "patient_id":caseModel?.patient_id ?? 0,
            "doctor_ids":[doctorId],
            "case_id":[caseModel?.id ?? 0]])) {[self] item in
                MYGLoading.hide()
                MMYGHUD.showToast(text: "分享成功")
                self.navigationController?.popViewController(animated: true)
            } failed: { _, error in
                MYGLoading.hide()
                MMYGHUD.showToast(text: error)
            }
    }
//    @objc private func textValueChanged(){
//        NotificationCenter.default.post(name: NSNotification.Name.init("patientScreen"), object: ["screenStr":searchTextField.text])
//    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        NotificationCenter.default.post(name: NSNotification.Name.init("patientScreen"), object: ["screenStr":searchTextField.text])
        return true
    }
    private func initUI(){
        
        view.addSubview(searchTextField)
        searchTextField.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(15))
            make.right.equalToSuperview().offset(-SCALEWIDTH(15))
            make.height.equalTo(SCALEWIDTH(35))
            make.top.equalToSuperview().offset(SCALEWIDTH(15))
        }
        view.addSubview(segmentedView)
        segmentedView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(SCALEWIDTH(45))
            make.top.equalTo(searchTextField.snp.bottom)
        }
        view.addSubview(listContainerView)
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-50)
            make.top.equalTo(segmentedView.snp.bottom)
        }
        //关联listContainer
        segmentedView.listContainer = listContainerView
        
        view.addSubview(addPBtn)
        addPBtn.snp.makeConstraints { make in
            make.left.bottom.equalToSuperview()
            make.width.equalTo(screenWidth)
            make.height.equalTo(50)
        }
        view.layoutIfNeeded()
        addPBtn.addCAGradientLayerColor(colors: [RGBA(r: 73, g: 117, b: 236, a: 1).cgColor,RGBA(r: 151, g: 179, b: 255, a: 1).cgColor], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 1, y: 0))
    }
    private lazy var searchTextField: UITextField = {
        let tf = UITextField()
        tf.font = .regular(14)
        tf.textColor = .rgba(51, 51, 51)
        tf.backgroundColor = .rgba(243, 243, 243, 1)
        tf.layer.masksToBounds = true
        tf.layer.cornerRadius = 15
        tf.attributedPlaceholder = NSMutableAttributedString.rz.colorfulConfer(confer: { confer in
            confer.image(UIImage(named: "Chat/search"))
            confer.text(" 搜索")?.font(.regular(14)).textColor(.rgba(142, 142, 147, 1))
        })
        tf.delegate = self
        tf.returnKeyType = .search
        tf.textAlignment = .center
//        tf.addTarget(self, action: #selector(textValueChanged), for: .editingChanged)
        //tf.delegate = self
        return tf
    }()
    private lazy var listContainerView: JXSegmentedListContainerView = {
        let vi = JXSegmentedListContainerView(dataSource: self)
        return vi
    }()
    private lazy var segmentedView: JXSegmentedView = {
        let segmentedView = JXSegmentedView()
        segmentedView.delegate = self
        segmentedView.dataSource = dataSource
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = SCALEWIDTH(16)
        indicator.indicatorHeight = SCALEWIDTH(3)
        indicator.indicatorColor = .rgba(32, 92, 255, 1)
        segmentedView.indicators = [indicator]
        return segmentedView
    }()
    
    private lazy var dataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.titles = ["我的好友","平台医生"]
        dataSource.titleNormalFont = .regular(13)
        dataSource.titleNormalColor = .rgba(118, 118, 118, 1)
        dataSource.titleSelectedFont = .regular(16)
        dataSource.titleSelectedColor = .rgba(32, 92, 255, 1)
        return dataSource
    }()
    private lazy var addView:MYGPatientAddView = {
        let vi = MYGPatientAddView()
        return vi
    }()
    private lazy var addPBtn:UIButton = {
        let vi = UIButton()
        vi.setTitle("确认分享", for: .normal)
        vi.titleLabel?.font = .medium(17)
        vi.setTitleColor(.white, for: .normal)
        vi.addTarget(self, action: #selector(sureAction), for: .touchUpInside)
        return vi
    }()

}
extension MYGDoctorSelectViewController: JXSegmentedViewDelegate{
    //点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，而不关心具体是点击还是滚动选中的情况。
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        currrentIndex = index
    }
    // 点击选中的情况才会调用该方法
    func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {}
    // 滚动选中的情况才会调用该方法
    func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {}
    // 正在滚动中的回调
    func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) {}
}
extension MYGDoctorSelectViewController:JXSegmentedListContainerViewDataSource{
    //返回列表的数量
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return 2
    }
    //返回遵从`JXSegmentedListContainerViewListDelegate`协议的实例
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {

        if index == 0{
            let vc = MYGDoctorSelectDetailListViewController()
            vc.caseModel = caseModel
            vc.isMyFriend = index == 0 ? true : false
            leftVc = vc
            return vc
        }else{
            let vc = MYGSelectAllDorctorsViewController()
            vc.caseModel = caseModel
            vc.isMyFriend = index == 0 ? true : false
            rightVc = vc
            return vc
        }
    }
    
}
